//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Internals;

namespace OpenLS.Drawing
{
    internal class Path2D : IOfficeXmlSerializable
    {
        private ObservableCollection<PathElement> elements = new ObservableCollection<PathElement>();

        public ObservableCollection<PathElement> Elements
        {
            get { return elements; }
        }

        internal PathFigure PathFigure
        {
            get
            {
                PathFigure f = new PathFigure();
                foreach (PathElement e in Elements)
                {
                    if (e is Path2DMoveTo)
                    {
                        Path2DMoveTo pe = (Path2DMoveTo) e;
                        f.StartPoint = new Point(
                            EmuConverter.FromEmu(long.Parse(pe.Point.X)),
                            EmuConverter.FromEmu(long.Parse(pe.Point.Y)));
                        continue;
                    }
                    if (e is Path2DClose)
                    {
                        f.IsClosed = true;
                        break;
                    }
                    f.Segments.Add(e.Segment);
                }
                return f;
            }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("path"))
            {
                //\\missing attributes
                PathElement p = PathElement.ReadPathElement(c);
                while (p != null)
                {
                    Elements.Add(p);
                    p = PathElement.ReadPathElement(c);
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("path"))
            {
                foreach (PathElement e in Elements)
                    c.WriteElement(e);
            }
        }

        #endregion
    }
}